//
//  JZGCarSaleImageCellFrame.m
//  JingZhenGu
//
//  Created by Gaowl on 15/6/5.
//  Copyright (c) 2015年 Mars. All rights reserved.
//

#import "JZGCarSaleImageCellFrame.h"

@implementation JZGCarSaleImageCellFrame

- (void)setCellModel:(JZGCarSaleImageCellModel *)cellModel
{
    _cellModel = cellModel;
    
    CGFloat marginL = kWConversions(15) ;
    CGFloat padding = kWConversions(20) ;

    // 图片容器
    CGFloat maxW = cellModel.maxWidth>0?cellModel.maxWidth:SCREEN_WIDTH;
    CGFloat imageContainerViewX = padding;
    CGFloat imageContainerViewY = 0;
    CGFloat imageContainerViewW = maxW - imageContainerViewX - marginL;
    
    CGFloat rowMaxImg = 3;
    // 按钮宽度
    CGFloat btnW = (imageContainerViewW - (rowMaxImg-1)*padding)/rowMaxImg;
    // 按钮高度
    CGFloat btnH = btnW;
    
    CGFloat addImageButtonW = btnW;
    CGFloat addImageButtonH = btnH;
    CGFloat addImageButtonX = 0;
    CGFloat addImageButtonY = 0;
    
    CGFloat imageContainerViewH = 0;
    
    CGRect lastImageFrame;
    
    NSInteger imageCount = cellModel.imageArray ? cellModel.imageArray.count : 0;
    if (imageCount != 0)
    {
        NSInteger row = 0;
        NSInteger col = 0;
        
        CGFloat imageViewX = 0;
        CGFloat imageViewY = 0;
        CGFloat imageViewW = 0;
        CGFloat imageViewH = 0;
        
        for (NSInteger i = 0; i < imageCount; i++) {
            col = i % 3;
            row = i / 3;
            imageViewW = btnW;
            imageViewH = btnH;
            imageViewX = col*padding + col*btnW;
            imageViewY = row*btnH + row*padding;
            CGRect imageFrame = CGRectMake(imageViewX, imageViewY, imageViewW, imageViewH);
            [self.imageViewsFrames addObject:[NSValue valueWithCGRect:imageFrame]];
            if (i == imageCount-1) {
                lastImageFrame = [[self.imageViewsFrames lastObject] CGRectValue];
            }
        }
        if (imageCount < self.totalCount) {
            if (col != rowMaxImg-1) {
                addImageButtonX = CGRectGetMaxX(lastImageFrame)+padding;
                addImageButtonY = row*btnH + row*padding;
                imageContainerViewH = CGRectGetMaxY(lastImageFrame);
            }else {
                addImageButtonX = 0;
                addImageButtonY = (row+1) * (btnH+padding);
                imageContainerViewH = addImageButtonY+addImageButtonH;
            }
        }else{
            imageContainerViewH = CGRectGetMaxY(lastImageFrame);
        }
    }
    self.addImageButtonFrame = CGRectMake(addImageButtonX, addImageButtonY, addImageButtonW, addImageButtonH);
    
    if (imageCount == 0) {
        imageContainerViewH = CGRectGetMaxY(self.addImageButtonFrame);
    }
    self.imageContainerViewFrame = CGRectMake(imageContainerViewX, imageContainerViewY, imageContainerViewW, imageContainerViewH);
    self.cellHeight = CGRectGetMaxY(self.imageContainerViewFrame)+padding;
    if (self.cellHeight < CGRectGetMaxY(self.msgLabelFrame)) {
        self.cellHeight = CGRectGetMaxY(self.msgLabelFrame)+padding;
    }
}

- (void)setTotalCount:(NSInteger)totalCount{
    _totalCount = totalCount;
}

- (NSMutableArray *)imageViewsFrames
{
    if (!_imageViewsFrames) {
        _imageViewsFrames = [NSMutableArray array];
    }
    return _imageViewsFrames;
}

@end
